package com.sllp.C71S3TsllpShop.web.back;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.Image;
import com.sllp.C71S3TsllpShop.bean.ImageExample;
import com.sllp.C71S3TsllpShop.bean.OrdersItem;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.bean.ProductExample;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.SmallType;
import com.sllp.C71S3TsllpShop.bean.SmallTypeExample;
import com.sllp.C71S3TsllpShop.bean.Specs;
import com.sllp.C71S3TsllpShop.bean.SpecsExample;
import com.sllp.C71S3TsllpShop.dao.BigTypeMapper;
import com.sllp.C71S3TsllpShop.dao.ImageMapper;
import com.sllp.C71S3TsllpShop.dao.ProductMapper;
import com.sllp.C71S3TsllpShop.dao.SmallTypeMapper;
import com.sllp.C71S3TsllpShop.dao.SpecsMapper;

@Controller
public class BackProductFindAction {

	@Resource
	BigTypeMapper btm;
	
	@Resource 
	SmallTypeMapper stm;
	
	@Resource 
	ProductMapper pm;
	
	@Resource
	SpecsMapper sm ;
	
	@Resource
	ImageMapper im;
	
	
	@GetMapping("back/toProductFind.do")
	public String toProductFind(@SessionAttribute(name="backLoginBusiness") Business business,
			Model m) {
		List<BigType> list=new ArrayList<BigType>();
		list=btm.selectByExample(null);
		m.addAttribute("bidtype", list);
		ProductExample pe =new ProductExample();
		pe.createCriteria().andStateEqualTo(2).andBusinessEqualTo(business.getId());
		PageHelper.startPage(1, 6);
		List<Product> productlist=pm.selectByExampleWithBLOBs(pe);
		System.out.println(productlist);
		m.addAttribute("productList", productlist);
		return "back/product-find::productFind";
	}
	
	@PostMapping("back/findProductsmalltype.do")
	public String findProduct(Model m,int bigType) {
		
		//根据大类型id获取小类型
		List<SmallType> smalllist =new ArrayList<SmallType>();
		//SmallType smallType =new SmallType();
		SmallTypeExample stb =new SmallTypeExample();
		stb.createCriteria().andParentEqualTo(bigType);
		smalllist=stm.selectByExample(stb);
		m.addAttribute("smalltype", smalllist);
		return "back/product-find::smallTypeSelect";
	}
	
	@PostMapping("back/productFindByCondition.do")
	public String findProduct(@RequestParam(required=false) String productName,
			@RequestParam(required=false) Integer smallType,
			@RequestParam(required=false) BigDecimal prePrice,
			@RequestParam(required=false) BigDecimal nextPrice,
			@SessionAttribute(name="backLoginBusiness") Business business,
			Model m){
	
		List<Product>Productlist =new ArrayList<Product>() ;
		ProductExample pe =new ProductExample();
		
		if(productName!=null && productName!="") {
			pe.createCriteria().andNameLike("%"+productName+"%");
		}
		if(prePrice!=null&&nextPrice!=null) {
			pe.createCriteria().andMarketPriceBetween(prePrice, nextPrice);
		}
		if(smallType!=null && smallType != 0) {
			
			pe.createCriteria().andTypeEqualTo(smallType);
			
		}
		
		pe.createCriteria().andBusinessEqualTo(business.getId()).andStateEqualTo(2);
		
		PageHelper.startPage(1, 6);
		Productlist=pm.selectByExampleWithBLOBs(pe);
		m.addAttribute("productList", Productlist);
		
		return "back/product-find::productListTable";
	}
	
	@PostMapping("back/productShowToPage.do")
	public String ordersItemShowToPage(@RequestParam(required=false) String productName,
			@RequestParam(required=false) Integer smallType,
			@RequestParam(required=false) BigDecimal prePrice,
			@SessionAttribute(name="backLoginBusiness") Business business,
			@RequestParam(required=false) BigDecimal nextPrice,int pageNum,Model m) {
		
		List<Product>Productlist =new ArrayList<Product>() ;
		ProductExample pe =new ProductExample();
		if(productName!=null) {
			pe.createCriteria().andNameLike("%"+productName+"%");
		}
		if(smallType!=null) {
			pe.createCriteria().andTypeEqualTo(smallType);
		}if(prePrice!=null&&nextPrice!=null) {
			pe.createCriteria().andMarketPriceBetween(prePrice, nextPrice);
		}
		
		pe.createCriteria().andBusinessEqualTo(business.getId()).andStateEqualTo(2);
		PageHelper.startPage(pageNum, 6);
		Productlist=pm.selectByExampleWithBLOBs(pe);
		

		m.addAttribute("productList", Productlist);
		
		return "back/product-find::productListTable";
	}
	
	@PostMapping("back/toLowerShelfProduct.do")
	public String  toLowerShelfProduct(@SessionAttribute(name="backLoginBusiness") Business business,Model m,int id) {
		int ok=0 ;
		ProductExample pe =new ProductExample();
		pe.createCriteria().andIdEqualTo(id);
		Product p =new Product();
		//将状态改为未启用
		p.setState(1);
		ok=pm.updateByExampleSelective(p, pe);
		toProductFind(business,m);
		return "back/product-find::productListTable";
	}
	
	
	@PostMapping("back/productModify.do")
	public String  productModify(Model m,int id) {
		Product p =new Product();
		p=pm.backModifyProduct(id);
		SmallType st=stm.selectByPrimaryKey(p.getType());
		BigType bt =btm.selectByPrimaryKey(st.getParent());
		//名称 优惠 商品描述
		m.addAttribute("product",p);
		
		//大类型 
		m.addAttribute("bigtype",bt);
		//小类型
		m.addAttribute("smalltype",st);
		
		//规格
		SpecsExample se =new SpecsExample();
		se.createCriteria().andProductEqualTo(id);
		List<Specs> spList =sm.selectByExampleToModifyProduct(se);
		m.addAttribute("splist",spList);
		
		//图片
		ImageExample ie =new ImageExample();
		ie.createCriteria().andProductEqualTo(id);
		List<Image> image = im.selectByExample(ie);
		m.addAttribute("image",image);
	
		return "back/product-add::productAdd";
	}
} 
